#include "scripteditorwidget.h"
#include "codeedit.h"

#include <QDebug>
#include <QVBoxLayout>
#include <QRegExp>

ScriptEditorWidget::ScriptEditorWidget(QWidget *parent) :
    QWidget(parent)
{
    editor = new CodeEdit();
    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->setContentsMargins(0,0,0,0);
    mainLayout->addWidget(editor);

    setLayout(mainLayout);
}

ScriptEditorWidget::~ScriptEditorWidget()
{
    delete editor;
}

void ScriptEditorWidget::setScriptText(const QString &scriptText)
{
    editor->setPlainText(scriptText);
}

QString ScriptEditorWidget::scriptText() const
{
    return editor->toPlainText();
}

void ScriptEditorWidget::appendFunction(const QString &funcName, const QStringList &params)
{
    QString fn = funcName;
    fn.remove(QRegExp("\\(.*\\)"));
    QString func = "function "+fn + "(";
    for(int i = 0; i<params.size(); i++)
    {
        if(i>0)
            func += ", ";
        func += params[i];
    }
    func += ")\n{\n}\n";
    editor->append(func);
    editor->findFunctionDeclaration(fn);
}
